﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace COC.Commons
{
    public class PlanTaskManagerConfig
    {
        private readonly XmlDocument _ConfigXml = new XmlDocument();
        public readonly List<string> TaskState;
        public readonly List<string> TaskType;
        public readonly string SyncPath = "";

        public PlanTaskManagerConfig()
        {
            _ConfigXml.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CodingPlan\\PlanTaskManagerConfig.xml"));
            TaskState = new List<string>();
            TaskType = new List<string>();
            TaskState = FreshList("TaskState");
            TaskType = FreshList("TaskType");
            SyncPath = GetSyncPath("SyncPath");
        }

        private List<string> FreshList(string xpath)
        {
            var map = new List<string>();
            var srcElement = _ConfigXml.DocumentElement?.SelectSingleNode(xpath);
            if (srcElement != null)
            {
                foreach (XmlElement ele in srcElement.ChildNodes)
                {
                    var text = ele.InnerText;
                    map.Add(text);
                }
            }
            return map;
        }

        private string GetSyncPath(string syncpath)
        {
            var path = "";
            var srcElement = _ConfigXml.DocumentElement?.SelectSingleNode(syncpath);
            if (srcElement != null) path = srcElement.InnerText;
            return path;
        }
    }
}
